package com.weilan.ark3.controller;


import com.weilan.ark3.buffer.DataArray;
import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.entity.ControlDto;
import com.weilan.ark3.entity.SocDto;
import com.weilan.ark3.utils.SocUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import static com.weilan.ark3.buffer.OverallSituation.OverallEMSData;

@RestController
@CrossOrigin
public class SocRegionController {
    /*
    SOC区间控制策略
    * */
    public static void socRegionCtl() {
        System.out.println("sssss" + GlobalParam.fengguZero);
        OverallEMSData = DataArray.slaveDataMap.get(1);
        //获取系统的SOC
        short sys_soc = OverallEMSData[26];
        short sys_pow = OverallEMSData[6];
        ControlDto controlDto = new ControlDto();
        //获取 MIN、SOC_L 、SOC_H 、MAX
        SocDto socDto = SocUtils.query();
        if (socDto != null) {
            int min_soc = socDto.getMin_soc();
            int bcp_soc = socDto.getBcp_soc();
            int dcm_soc = socDto.getDcm_soc();
            int max_soc = socDto.getMax_soc();
            //校验以上参数的合法性
            if ((min_soc < max_soc) && (bcp_soc <= dcm_soc)) {
                //根据设置的参数判断出所支持的模式
                //根据SOC所在的区间，判断出系统运行的模式
                if (bcp_soc < dcm_soc) {
                    //备电 + 光伏 + 峰谷
                    if ((bcp_soc <= sys_soc) && (sys_soc < dcm_soc)) {
                        //峰谷
                        PeakValleyController.fengguCtl();

                    } else if ((min_soc <= sys_soc) && (sys_soc < bcp_soc)) {
                        //备电模式
                        BackupController.backupModeSoc();
                        System.out.println("执行备电模式");
                    } else if ((dcm_soc <= sys_soc) && (sys_soc < max_soc)) {
                        //PV模式
                        PvController.Spontaneous_pv();
                        //PeakValleyController.fengguCtl();
                    } else if (sys_soc >= max_soc) {
                        //soc>max禁止充电
                        if (sys_pow < 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    } else if (sys_soc < min_soc) {
                        //SOC< min禁止放电
                        if (sys_pow > 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    }
                } else if (min_soc == bcp_soc) {
                    //光伏 + 峰谷
                    if ((min_soc <= bcp_soc) && (sys_soc < dcm_soc)) {
                        //光伏
                        //System.out.println("运行光伏自发自用模式");
                        //PvController.Spontaneous_pv();

                        //峰谷
                        PeakValleyController.fengguCtl();
                    } else if ((dcm_soc <= sys_soc) && (sys_soc < max_soc)) {
                        //峰谷
                        //PeakValleyController.fengguCtl();
                        //光伏
                        //System.out.println("运行光伏自发自用模式");
                        PvController.Spontaneous_pv();
                    } else if (sys_soc >= max_soc) {
                        //soc>max禁止充电
                        if (sys_pow < 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    } else if (sys_soc < min_soc) {
                        //SOC< min禁止放电
                        if (sys_pow > 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    }

                } else if (dcm_soc == max_soc) {
                    //备电 + 峰谷
                    if ((min_soc <= sys_soc) && (sys_soc < bcp_soc)) {
                        //备电
                        BackupController.backupModeSoc();
                    } else if ((min_soc <= bcp_soc) && (sys_soc < dcm_soc)) {
                        //光伏
                        //System.out.println("运行光伏自发自用模式");
                        //PvController.Spontaneous_pv();
                        //峰谷
                        PeakValleyController.fengguCtl();

                    } else if (sys_soc >= max_soc) {
                        //soc>max禁止充电
                        if (sys_pow < 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    } else if (sys_soc < min_soc) {
                        //SOC< min禁止放电
                        if (sys_pow > 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    }
                } else if (bcp_soc == dcm_soc) {
                    //备电 + 光伏
                    if ((min_soc <= sys_soc) && (sys_soc < bcp_soc)) {
                        //备电
                        BackupController.backupModeSoc();
                    } else if ((dcm_soc <= sys_soc) && (sys_soc < max_soc)) {
                        //峰谷
                        //PeakValleyController.fengguCtl();

                        //光伏
                        //System.out.println("运行光伏自发自用模式");
                        PvController.Spontaneous_pv();
                    } else if (sys_soc >= max_soc) {
                        //soc>max禁止充电
                        if (sys_pow < 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    } else if (sys_soc < min_soc) {
                        //SOC< min禁止放电
                        if (sys_pow > 0) {
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(0);
                            MsgQueue.writeToControlTcp(controlDto);
                        }
                    }
                }
            } else if ((min_soc == bcp_soc) && (dcm_soc == max_soc)) {
                //峰谷模式
                //PeakValleyController.fengguCtl();
                //光伏
                //System.out.println("运行光伏自发自用模式");
                PvController.Spontaneous_pv();
            } else if ((min_soc == bcp_soc) && (dcm_soc == bcp_soc)) {
                //光伏自发自用
                //System.out.println("运行光伏自发自用模式");
                //PvController.Spontaneous_pv();
                //峰谷模式
                PeakValleyController.fengguCtl();
            } else if ((dcm_soc == bcp_soc) && (dcm_soc == max_soc)) {
                //备电
                BackupController.backupModeSoc();
            } else if (sys_soc >= max_soc) {
                //soc>max禁止充电
                if (sys_pow < 0) {
                    controlDto.setSlaveId(GlobalParam.guangbo);
                    controlDto.setAddress(GlobalParam.power);
                    controlDto.setValue(0);
                    MsgQueue.writeToControlTcp(controlDto);
                }
            } else if (sys_soc < min_soc) {
                //SOC< min禁止放电
                if (sys_pow > 0) {
                    controlDto.setSlaveId(GlobalParam.guangbo);
                    controlDto.setAddress(GlobalParam.power);
                    controlDto.setValue(0);
                    MsgQueue.writeToControlTcp(controlDto);
                }
            }
        }
    }
}

